//
//  YSAddressCtrl.m
//  Yueshi
//
//  Created by Northwan on 15/7/29.
//  Copyright (c) 2015年 悦食中国. All rights reserved.
//  地址

#import "YSAddressCtrl.h"
#import "YSAddressCell.h"
#import "YSLocationDao.h"
#import "YSAddressDBModel.h"
#import "YSNewAddressCtrl.h"
#import "YSMineDataTool.h"


@interface YSAddressCtrl ()<UITableViewDataSource,UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (strong, nonatomic) YSLocationDao *locationDao;
@property (strong, nonatomic) NSMutableArray *addressArray;
@property (assign, nonatomic) AddressSourceType type;
@property (strong, nonatomic) UILabel *nullPageLabel;
@property (strong, nonatomic) UIImageView *emptyImgView;
@end

@implementation YSAddressCtrl


-(NSMutableArray *)addressArray{
    if(!_addressArray){
        _addressArray = [NSMutableArray array];
    }
    return _addressArray;
}

-(BOOL)shouldLogin{
    return YES;
}

-(instancetype)initWithType:(AddressSourceType)type{
    self = [super init];
    if(self){
         self.type = type;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.navigationItem setTitle:@"收货地址"];
    
    _nullPageLabel = [UIUtil getPageTipLabelWithTipText:@"还没收货地址 快来添加哦"];
    [_nullPageLabel setHidden:YES];
    [self.view addSubview:_nullPageLabel];
    
    UIImage *emptyImg = [UIImage imageNamed:@"bg_address_empty"];
    _emptyImgView = [[UIImageView alloc]initWithImage:emptyImg];
    [_emptyImgView setX:(SCREENW-emptyImg.size.width)/2];
    [_emptyImgView setY:_nullPageLabel.y-emptyImg.size.height-YSPublicXInset*2] ;
    [_emptyImgView setHidden:YES];
    [self.view addSubview:_emptyImgView];
    
    
    
    self.locationDao = [[YSLocationDao alloc]init];
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(itemChange:) name:kAddressItemChangeNortif object:nil];
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc]initWithTitle:@"新增" style:UIBarButtonItemStylePlain target:self action:@selector(addAddress:)];
    rightItem.tintColor = ThemeColor;
    self.navigationItem.rightBarButtonItem = rightItem;
    
    [self.tableView setDelegate:self];
    [self.tableView setDataSource:self];
    [self.tableView setBackgroundColor:PUBLIC_BG];
    [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(headerRefresh)];
    header.lastUpdatedTimeLabel.hidden = YES;
    _tableView.header = header;
    [self.tableView setTableFooterView:[UIView new]];
    [self.tableView.header beginRefreshing];
}

-(void)reqeustData{
    [YSMineDataTool getAddressList:^(YSAddressRes *result) {
         [UIUtil refreshComplete:self.tableView];
        if(result.isSucc){
            [self.addressArray removeAllObjects];
            [self.addressArray addObjectsFromArray:result.adresList];
            [self.tableView reloadData];
            
            if(!self.addressArray.count){
                [_nullPageLabel setHidden:NO];
                [_emptyImgView setHidden:NO];
            }else{
                [_nullPageLabel setHidden:YES];
                [_emptyImgView setHidden:YES];
            }
        }else{
            ShowEnterTips(result.data);
        }
    } failure:^(NSError *error) {
        [UIUtil refreshComplete:self.tableView];
        ShowTips(kRequestFailure);
    }];
}

-(void)headerRefresh{
    if(ISLOGIN){
        [self reqeustData];
    }else{
        [UIUtil refreshComplete:self.tableView];
    }
}

-(void)itemChange:(NSNotification *)nortif{
    [self reqeustData];
}

-(void)addAddress:(UIBarButtonItem *)item{
    [self.navigationController pushViewController:[[YSNewAddressCtrl alloc]initWithType:NewAddressSourceMineType] animated:YES];
}

#pragma - mark UITableViewDataSource,UITableViewDelegate
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _addressArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    YSAddressCell *cell =  [YSAddressCell initWithTableView:tableView];
    [cell setAddress:self.addressArray[indexPath.row]];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return YSAddressCellHeight;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    YSAddress *address =   self.addressArray[indexPath.row];
    BOOL status = [address defaultAdres];
    NSInteger addressId = [address addressId] ;
    if(self.type == AccountCenterType){
        [address setAddressId:addressId];
        [address setAddressee:[address addressee]];
        [address setPhoneCode:[address phoneCode]];
        [address setDeliveryId:[address deliveryId]];
        [address setDetailAdres:[address detailAdres]];
        [address setAddressStr:[address addressStr]];
        [address setPostal:[address postal]];
        [[NSNotificationCenter defaultCenter]postNotificationName:kChooseAddressFinishNortif object:address];
        [self.navigationController popViewControllerAnimated:YES];
    }else{
        if(!status){
            [YSMineDataTool setDefaultWithAddressId:addressId success:^(BaseResponse *result) {
                if(result.isSucc){
                    [self reqeustData];
                    [self.tableView reloadData];
                }else{
                    ShowEnterTips(result.data);
                }
            } failure:^(NSError *error) {
                ShowTips(kRequestFailure);
            }];
        }
    }
}

-(void)dealloc{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}

@end
